package com.ruoyi.web.controller.aggregation;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.aggregation.dto.LoginReqDTO;
import com.ruoyi.common.core.domain.aggregation.dto.PageDTO;
import com.ruoyi.common.core.domain.aggregation.dto.UserRespDTO;
import com.ruoyi.system.domain.aggregation.User;
import com.ruoyi.system.service.aggregation.IAggUserService;
import com.ruoyi.common.utils.SecurityUtils;

/**
 * 聚合码用户Controller
 * 
 * @author ruoyi
 * @date 2025-01-01
 */
@RestController
@RequestMapping("/agg/user")
public class AggUserController
{
    @Autowired
    private IAggUserService userService;

    /**
     * Web管理后台登录（账号密码）
     */
    @Anonymous
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginReqDTO loginReq)
    {
        try
        {
            UserRespDTO userResp = userService.login(loginReq);
            return AjaxResult.success(userResp);
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * App小程序登录（手机验证码）
     */
    @Anonymous
    @PostMapping("/appLogin")
    public AjaxResult appLogin(@RequestBody LoginReqDTO loginReq)
    {
        try
        {
            UserRespDTO userResp = userService.appLogin(loginReq);
            return AjaxResult.success(userResp);
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 忘记密码
     */
    @Anonymous
    @PostMapping("/forget")
    public AjaxResult forget(@RequestBody LoginReqDTO loginReq)
    {
        try
        {
            userService.forgetPassword(loginReq);
            return AjaxResult.success();
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 重置密码
     */
    @PostMapping("/reset")
    public AjaxResult reset(@RequestBody LoginReqDTO loginReq)
    {
        try
        {
            userService.resetPassword(loginReq);
            return AjaxResult.success();
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 退出登录
     */
    @PostMapping("/loginOut")
    public AjaxResult loginOut(@RequestParam String phone)
    {
        try
        {
            userService.loginOut(phone);
            return AjaxResult.success();
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 查询商家列表
     */
    @PostMapping("/merchantPage")
    public AjaxResult merchantPage(@RequestBody PageDTO pageDTO)
    {
        try
        {
            PageHelper.startPage(pageDTO.getPageIndex().intValue(), pageDTO.getPageSize().intValue());
            User query = new User();
            query.setType(3); // 商家
            List<User> list = userService.selectUserList(query);
            PageInfo<User> pageInfo = new PageInfo<>(list);
            return AjaxResult.success(pageInfo);
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 查询业务员列表
     */
    @PostMapping("/saleManPage")
    public AjaxResult saleManPage(@RequestBody PageDTO pageDTO)
    {
        try
        {
            PageHelper.startPage(pageDTO.getPageIndex().intValue(), pageDTO.getPageSize().intValue());
            User query = new User();
            query.setType(2); // 业务员
            List<User> list = userService.selectUserList(query);
            PageInfo<User> pageInfo = new PageInfo<>(list);
            return AjaxResult.success(pageInfo);
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 查询区域代理列表
     */
    @PostMapping("/regionPage")
    public AjaxResult regionPage(@RequestBody PageDTO pageDTO)
    {
        try
        {
            PageHelper.startPage(pageDTO.getPageIndex().intValue(), pageDTO.getPageSize().intValue());
            User query = new User();
            query.setType(4); // 区域代理
            List<User> list = userService.selectUserList(query);
            PageInfo<User> pageInfo = new PageInfo<>(list);
            return AjaxResult.success(pageInfo);
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 新增业务员
     */
    @PostMapping("/addSaleMan")
    public AjaxResult addSaleMan(@RequestBody User user)
    {
        try
        {
            user.setType(2); // 业务员
            userService.insertUser(user);
            return AjaxResult.success();
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 新增区域代理
     */
    @PostMapping("/addRegion")
    public AjaxResult addRegion(@RequestBody User user)
    {
        try
        {
            user.setType(4); // 区域代理
            userService.insertUser(user);
            return AjaxResult.success();
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 编辑用户
     */
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody User user)
    {
        try
        {
            userService.updateUser(user);
            return AjaxResult.success();
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 删除业务员
     */
    @GetMapping("/delete")
    public AjaxResult delete(@RequestParam Long id)
    {
        try
        {
            userService.deleteUserById(id);
            return AjaxResult.success();
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 获取用户详情
     */
    @GetMapping("/getUserInfo")
    public AjaxResult getUserInfo(@RequestParam(required = false) Long id)
    {
        try
        {
            User user;
            if (id != null) {
                // 传了id，获取指定用户信息
                user = userService.selectUserById(id);
            } else {
                // 没传id，获取当前登录用户信息
                Long currentUserId = SecurityUtils.getUserId();
                if (currentUserId == null) {
                    return AjaxResult.error("用户未登录");
                }
                user = userService.selectUserById(currentUserId);
            }
            return AjaxResult.success(user);
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 获取当前登录用户信息
     */
    @GetMapping("/current")
    public AjaxResult getCurrentUser()
    {
        try
        {
            Long currentUserId = SecurityUtils.getUserId();
            if (currentUserId == null) {
                return AjaxResult.error("用户未登录");
            }
            User user = userService.selectUserById(currentUserId);
            return AjaxResult.success(user);
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 用户详情接口（别名，兼容前端调用）
     */
    @GetMapping("/detail")
    public AjaxResult detail(@RequestParam(required = false) Long id)
    {
        try
        {
            User user;
            if (id != null) {
                // 传了id，获取指定用户信息
                user = userService.selectUserById(id);
            } else {
                // 没传id，获取当前登录用户信息
                Long currentUserId = SecurityUtils.getUserId();
                if (currentUserId == null) {
                    return AjaxResult.error("用户未登录");
                }
                user = userService.selectUserById(currentUserId);
            }
            return AjaxResult.success(user);
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }
}

